<template>
  <div class="commission">
    <dt-search ref="search" :xl="6" :xlBtn="6" @reset="resetFun">
      <Button type="primary" v-if="unlocking('0210030100')" @click="onPayment">发起佣金回款</Button>
    </dt-search>

    <dt-grid ref="table" page :onFetch="onFetch" />

    <!-- 发起佣金回款 -->
    <payment-modal ref="payment" @success="onSuccess" />
    <!-- 查看详情 -->
    <detail-modal ref="detail" />
  </div>
</template>

<script>
  import paymentModal from './components/payment-modal.vue'
  import detailModal from './components/detail-modal.vue'
  import Column from './opt/Column.js'
  import SearchOpt from './opt/SearchOpt.js'
  import paymentMgr from '@/biz/commission/payment.js'

  let backId = null
  export default {
    components: {
      paymentModal,
      detailModal
    },
    data() {
      return {
      }
    },
    mounted() {
      backId = this.$route.params.businessId || backId
      this.$refs.table.init(this, new Column())
      this.$refs.search.init(this.$refs.table, new SearchOpt())
      this.$refs.table.fetch()
    },
    methods: {
      onFetch(data) {
        return paymentMgr
          .list({
            ...data,
            orgId: data.orgId ? data.orgId.join(',') : null,
            amountFlag: false,
            backId: backId
          })
          .then((res) => {
            return res
          })
      },
      // 重置待办任务的传参
      resetFun(){
        backId = null
      },
      // 发起佣金回款
      onPayment() {
        this.$refs.payment.create()
      },
      // 查看详情
      onDetail(data) {
        this.$refs.detail.create(data)
      },
      onUpload(data) {
        this.$refs.payment.create(data.backId)
      },
      onDelete(data) {
        this.$Modal.confirm({
          title: '请确认是否删除该条回款？',
          onOk: () => {
            paymentMgr.del(data.backId).then((res) => {
              this.$Message.success('删除成功')
              this.$refs.table.fetch()
            })
          }
        })
      },
      onSuccess() {
        this.$refs.table.fetch()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .commission {
    padding: 20px;
  }
</style>
